/**
 * 
 */
package com.laotou99.backjack.util;

import java.awt.Image;  
import java.awt.color.ColorSpace;  
import java.awt.image.BufferedImage;  
import java.awt.image.ColorConvertOp;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
  
import javax.imageio.ImageIO;  
  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
/**
 * @author wisdom
 *
 */
public class ImageUtil1 {

	/**
	 * 
	 */
	public ImageUtil1() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

//        File file=new File("C:\\Documents and Settings\\Administrator\\桌面\\cd.jpg");  
        File file=new File("C:\\tools\\abc2.jpg");  
        changeImge(file);         

	}

    /** 	
     * * 转换图片 * * 
     */  
    public static void changeImge(File img) {  
        try {  
            Image image = ImageIO.read(img);  
            int srcH = image.getHeight(null);  
            int srcW = image.getWidth(null);  
//            BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_3BYTE_BGR);  
            BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_BYTE_GRAY);  
//            BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_USHORT_GRAY);  
            bufferedImage.getGraphics().drawImage(image, 0,0, srcW, srcH, null);  
            bufferedImage=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),null).filter (bufferedImage,null);   
            FileOutputStream fos = new FileOutputStream(img);  
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);  
            encoder.encode(bufferedImage);  
            fos.close();  
             System.out.println("转换成功...");  
             System.out.println("ok");  
        } catch (IOException e) {  
            e.printStackTrace();  
            throw new IllegalStateException("图片转换出错！", e);  
        }  
    }  
  
}
